﻿using System.Windows.Forms;
using BrightIdeasSoftware;
using PictureOrganizer2.Database;
using System.Data.Linq;
using System.Linq;
using System.Data;
using System.Drawing;

namespace PictureOrganizer2.Forms
{
    public partial class EditComicTagsForm : Form
    {
        private int comidId = 0;

        public EditComicTagsForm()
        {
            InitializeComponent();

            this.Icon = Icon.FromHandle(Images.tag_blue_edit.GetHicon());

        }

        public EditComicTagsForm(int comicId)
            : this()
        {
            this.olvColumnRightName.AspectGetter = new AspectGetterDelegate(delegate(object x)
                {
                    var row = (DatabaseDataSet.ComicHasTagRow)((DataRowView)x).Row;

                    return (from t in dbSet.Tag where t.ID == row.TagID select t.Name).First();
                });


            this.comidId = comicId;
            this.tagTableAdapter.Fill(this.dbSet.Tag);
            this.comicHasTagTableAdapter.FillByComicId(this.dbSet.ComicHasTag, comicId);

            this.dlv_Left.UseFiltering = true;
            this.dlv_Left.ModelFilter = new ModelFilter(delegate(object x)
            {
                var row = (DatabaseDataSet.TagRow)((DataRowView)x).Row;
                var r = (from cht in dbSet.ComicHasTag where cht.RowState != DataRowState.Deleted select cht);

                return 0 == (from cht in r
                             where cht.TagID == row.ID && cht.ComicID == comicId
                             select cht).Count();
            });

            //this.dlv_Right.UseFiltering = true;
            //this.dlv_Right.ModelFilter = new ModelFilter(delegate(object x)
            //{
            //    var row = (DatabaseDataSet.TagRow)((DataRowView)x).Row;

            //    //return 1 == (from cht in dbSet.ComicHasTag
            //    //             where cht.TagID == row.ID && cht.ComicID == comicId && cht.RowState != DataRowState.Deleted
            //    //             select cht).Count();
            //    return true;
            //});
        }

        private void tsb_Add_Click(object sender, System.EventArgs e)
        {
            DatabaseDataSet.TagRow row = this.dbSet.Tag.NewTagRow();
            int nextId = (from t in dbSet.Tag orderby t.ID descending select t.ID).FirstOrDefault() + 1;
            row.ID = nextId;
            row.Name = "Neue Tag";
            this.dbSet.Tag.AddTagRow(row);
        }

        private void btn_Ok_Click(object sender, System.EventArgs e)
        {
            this.comicHasTagTableAdapter.Update(this.dbSet.ComicHasTag);
        }

        private void dataListView_CellClick(object sender, CellClickEventArgs e)
        {
            if (this.dlv_Left.SelectedObject != null)
            {
                this.tsb_Delete.Enabled = true;
            }
            else
            {
                this.tsb_Delete.Enabled = false;
            }

        }

        private void tsb_Delete_Click(object sender, System.EventArgs e)
        {
            if (this.dlv_Left.SelectedObject != null)
            {
                DatabaseDataSet.TagRow row = (DatabaseDataSet.TagRow)((DataRowView)this.dlv_Left.SelectedObject).Row;

                foreach (var item in (from cht in dbSet.ComicHasTag where cht.TagID == row.ID select cht))
                {
                    item.Delete();
                }
                row.Delete();
            }
        }

        private void btn_ToRight_Click(object sender, System.EventArgs e)
        {
            AddTag();
        }

        private void AddTag()
        {
            if (this.dlv_Left.SelectedObjects.Count > 0)
            {
                foreach (DataRowView item in this.dlv_Left.SelectedObjects)
                {
                    var row = this.dbSet.ComicHasTag.NewComicHasTagRow();
                    row.ComicID = this.comidId;
                    row.TagID = ((DatabaseDataSet.TagRow)item.Row).ID;
                    dbSet.ComicHasTag.AddComicHasTagRow(row);
                }
            }
            this.dlv_Left.BuildList(true);
            this.dlv_Right.BuildList(true);
        }

        private void btn_ToLeft_Click(object sender, System.EventArgs e)
        {
            RemoveTag();
        }

        private void RemoveTag()
        {
            if (this.dlv_Right.SelectedObjects.Count > 0)
            {
                foreach (DataRowView item in this.dlv_Right.SelectedObjects)
                {
                    var r = (from cht in dbSet.ComicHasTag where cht.RowState != DataRowState.Deleted select cht);
                    var row = (from cht in r
                               where cht.ComicID == this.comidId && cht.TagID == ((DatabaseDataSet.ComicHasTagRow)item.Row).TagID
                               select cht).First();

                    row.Delete();
                }
            }
            this.dlv_Left.BuildList(true);
            this.dlv_Right.BuildList(true);
        }

        private void dlv_Left_DoubleClick(object sender, System.EventArgs e)
        {
            AddTag();
        }

        private void dlv_Right_DoubleClick(object sender, System.EventArgs e)
        {
            RemoveTag();
        }
    }
}
